package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import lombok.Getter;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @author 毛文杰
 * 养殖端---生产中心-饲料记录
 */
@Data
public class FeedReport implements Serializable {
    private static final long serialVersionUID = -6467454221167070468L;
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    /**
     * id
     */
    private Long id;
    /**
     * 单据编号
     */
    private String number;
    /**
     * '状态, 1：正常，-1：删除'
     */
    private Integer status;
    /**
     * 养殖场id
     */
    private Long breedFactoryId;
    /**
     * 养殖场名称
     */
    private String  breedFactoryName;
    /**
     * 记录日期
     */
    private Date date;
    /**
     * 养殖区域 10：繁殖区, 20：保育区, 30：育肥区
     */
    private Integer Area;

    /**
     * 栏舍号id
     */
    private Long stableId;

    /**
     * 栏舍号
     */
    private String stableNumber;
    /**
     * 饲料记录饲料种类 1010：猪饲料 2010：鸡饲料 3010：鸭饲料 4010：羊饲料 9910：其他
     */
    private  Integer feedType;
    /**
     * 其他饲料种类
     */
    private  String otherType;
    /**
     * 单位
     */
    private  Integer unit;
    /**
     * 用量
     */
    private Float usedQuantity;

    private Map<String , Object> ingredientMap;
    /**
     * 配方
     */
    @JsonIgnore
    private String ingredientJson;
    /**
     * 生产厂家
     */
    private String manufacturer;
    /**
     * 生产日期
     */
    private Date manufacturerDate;
    /**
     * 饲料照片
     */
    @JsonIgnore
    private String feedPhoto;

    /**
     * 饲料照片,不存数据库
     */
    @Getter
    private Map<String,Map<String,String>> feed;
    /*
     *
     * 单据是否有效
     */
    private Integer deleteFlag;
    /**
     *
     */
    private Date createdAt;
    /**
     *
     */
    private Date updatedAt;

    public void setFeedPhoto(String feedPhoto){
        this.feedPhoto = feedPhoto;
        if(Strings.isNullOrEmpty(feedPhoto)){
            this.feed=  Maps.newHashMap();
        } else{
            try {
                this.feed = objectMapper.readValue(feedPhoto, new TypeReference<Map<String,Map<String,String>>>(){});
            } catch (IOException e) {
                // this is fuck exception
            }
        }
    }
    public void setFeed(Map<String,Map<String,String>> feed) {
        this.feed = feed;
        if(feed ==null ||feed.isEmpty()){
            this.feedPhoto = null;
        }else{
            try {
                this.feedPhoto = objectMapper.writeValueAsString(feed);
            } catch (Exception e) {
                // this is fuck exception
            }
        }
    }

    public void setIngredientMap(Map<String ,Object> ingredientMap){
        this.ingredientMap = ingredientMap;
        if(ingredientMap == null || ingredientMap.isEmpty()){
            this.ingredientJson = null;
        }else {
            try {
                this.ingredientJson = objectMapper.writeValueAsString(ingredientMap);
            } catch (JsonProcessingException e) {
                // this is fuck exception

            }
        }
    }

    public void setIngredientJson(String ingredientJson){
        this.ingredientJson = ingredientJson;
        if(Strings.isNullOrEmpty(ingredientJson)){
            this.ingredientMap = Maps.newHashMap();
        }else {
            try {
                this.ingredientMap = objectMapper.readValue(ingredientJson, new TypeReference<Map<String ,Object>>() {});
            } catch (IOException e) {
                // this is fuck exception
            }
        }
    }
}
